package org.jeecg.modules.demo.kcmx.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo.kcmx.entity.JxcKcmx;
import org.jeecg.modules.demo.kcmx.entity.JxcRuku;
import org.jeecg.modules.demo.kcmx.entity.JxcYfk;
import org.jeecg.modules.demo.kcmx.service.IJxcKcmxService;
import org.jeecg.modules.demo.kcmx.service.IJxcRukuService;
import org.jeecg.modules.demo.kcmx.service.IJxcYfkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

 /**
 * @Description: 应付款项
 * @Author: jeecg-boot
 * @Date:   2024-07-02
 * @Version: V1.0
 */
@Api(tags="应付款项")
@RestController
@RequestMapping("/fk/jxcYfk")
@Slf4j
public class JxcYfkController extends JeecgController<JxcYfk, IJxcYfkService> {
	@Autowired
	private IJxcYfkService jxcYfkService;
	@Autowired
	private IJxcRukuService jxcRukuService;
	 @Autowired
	 private IJxcKcmxService jxcKcmxService;

	/**
	 * 分页列表查询
	 *
	 * @param jxcYfk
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "应付款项-分页列表查询")
	@ApiOperation(value="应付款项-分页列表查询", notes="应付款项-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<JxcYfk>> queryPageList(JxcYfk jxcYfk,
											   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
											   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
											   HttpServletRequest req) {
		QueryWrapper<JxcYfk> queryWrapper = QueryGenerator.initQueryWrapper(jxcYfk, req.getParameterMap());
		Page<JxcYfk> page = new Page<JxcYfk>(pageNo, pageSize);
		IPage<JxcYfk> pageList = jxcYfkService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param jxcYfk
	 * @return
	 */
	@AutoLog(value = "应付款项-添加")
	@ApiOperation(value="应付款项-添加", notes="应付款项-添加")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody JxcYfk jxcYfk) {
		Date date = new Date();
		SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		SimpleDateFormat df2 = new SimpleDateFormat("yyyyMMddHHmmss");
		String id = "ZC"+df2.format(date);
		String zcOnly = jxcYfk.getZcOnly();
		String zcShiji = jxcYfk.getZcShiji();
		String zcNew = jxcYfk.getZcNew();
		String localtime = df1.format(date);
		JxcRuku jxcRuku = new JxcRuku();
		jxcRuku.setId(id);
		jxcRuku.setZcOnly(zcOnly);
		jxcRuku.setZcName(zcOnly);
		jxcRuku.setZcShiji(zcShiji);
//		jxcRuku.setZcYuji(zcShiji);
		jxcRuku.setZcOld(zcNew);
		jxcRuku.setCreatTime(localtime);
		jxcRuku.setCompanyId(jxcYfk.getCompanyId());
		jxcRukuService.save(jxcRuku);

		JxcKcmx jxcKcmx = jxcKcmxService.getByOnly(zcOnly, jxcYfk.getCompanyId());
		if (jxcKcmx == null) {
			JxcKcmx jxcKcmx1 = new JxcKcmx();
			jxcKcmx1.setId(id);
			jxcKcmx1.setZcOnly(zcOnly);
			jxcKcmx1.setZcName(zcOnly);
			jxcKcmx1.setZcLittle(zcOnly);
			jxcKcmx1.setZcRuku(zcShiji);
			jxcKcmx1.setZcChuku("0");
			jxcKcmx1.setZcKucun(zcShiji);
			jxcKcmx1.setZcOld(zcNew);
			jxcKcmx1.setZcSize("1");
			jxcKcmx1.setZcNew(zcNew);
			jxcKcmx1.setCompanyId(jxcYfk.getCompanyId());
			jxcKcmxService.save(jxcKcmx1);
		}else {
			String zcKucun = jxcKcmx.getZcKucun();
			String zcRuku = jxcKcmx.getZcRuku();
//			String newKucun = String.valueOf(Double.parseDouble(zcKucun)+Double.parseDouble(zcShiji));
//			String newRuku = String.valueOf(Double.parseDouble(zcRuku)+Double.parseDouble(zcShiji));
			BigDecimal zcKucunBD = new BigDecimal(zcKucun);
			BigDecimal zcShijiBD = new BigDecimal(zcShiji);
			BigDecimal zcRukuBD = new BigDecimal(zcRuku);

// 使用BigDecimal进行加法运算
			BigDecimal newKucunBD = zcKucunBD.add(zcShijiBD).setScale(0, RoundingMode.DOWN);
			BigDecimal newRukuBD = zcRukuBD.add(zcShijiBD).setScale(0, RoundingMode.DOWN);

// 转换为没有小数点的字符串
			String newKucun = newKucunBD.toPlainString();
			String newRuku = newRukuBD.toPlainString();
			jxcKcmxService.setRuku(zcOnly, newRuku,newKucun, jxcYfk.getCompanyId());
		}


		String zcOldpay = String.valueOf(Double.parseDouble(zcShiji)*Double.parseDouble(zcNew));
		jxcYfk.setId(id);
		jxcYfk.setZcOldpay(zcOldpay);
		jxcYfk.setZcTime(localtime);
		jxcYfk.setCompanyId(jxcYfk.getCompanyId());
		jxcYfkService.save(jxcYfk);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param jxcYfk
	 * @return
	 */
	@AutoLog(value = "应付款项-编辑")
	@ApiOperation(value="应付款项-编辑", notes="应付款项-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody JxcYfk jxcYfk) {
		System.out.println("jxcYfk"+jxcYfk);
		String id = jxcYfk.getId();
		String zcOnly = jxcYfk.getZcOnly();
		//修改后的数量
		String zcShiji = jxcYfk.getZcShiji();
		//修改后的单价
		String zcNew = jxcYfk.getZcNew();
		System.out.println("zcNew"+zcNew);
		JxcYfk oldyfk = jxcYfkService.getById(id);
		JxcRuku jxcRuku = jxcRukuService.getById(id);
		if (jxcRuku == null) {
			throw new IllegalStateException("jxcRuku is null for ID: " + id);
		}

		JxcKcmx jxcKcmx = jxcKcmxService.getByOnly(zcOnly,jxcYfk.getCompanyId());
		if (jxcKcmx == null) {
			throw new IllegalStateException("jxcKcmx is null for zcOnly: " + zcOnly);
		}
		//原来的数量
		String zcShiji1 = oldyfk.getZcShiji();
		BigDecimal zcShiji1BD = new BigDecimal(zcShiji1);
		BigDecimal zcShijiBD = new BigDecimal(zcShiji);
		//如果比原来的数量多
		if (zcShijiBD.compareTo(zcShiji1BD) > 0){
			BigDecimal slchazhiBD = zcShijiBD.subtract(zcShiji1BD).setScale(0, RoundingMode.HALF_UP);
			// 获取当前的 zcShiji 值并转换为 BigDecimal
			BigDecimal currentZcShijiBD = new BigDecimal(jxcRuku.getZcShiji());
			// 更新 jxcRuku 的 zcShiji 属性
			BigDecimal updatedZcShijiBD = currentZcShijiBD.add(slchazhiBD).setScale(0, RoundingMode.HALF_UP);
			// 将结果转换为字符串并设置到 jxcRuku 对象上
			jxcRuku.setZcShiji(updatedZcShijiBD.toPlainString());
			System.out.println("zcNew"+zcNew);
			jxcRuku.setZcOld(zcNew);
			jxcRukuService.updateById(jxcRuku);
			// 使用 BigDecimal 进行计算并调用 jxcKcmxService.setRuku 方法
			BigDecimal rukuBD = new BigDecimal(jxcKcmx.getZcRuku());
			BigDecimal kucunBD = new BigDecimal(jxcKcmx.getZcKucun());
			BigDecimal updatedRukuBD = rukuBD.add(slchazhiBD).setScale(0, RoundingMode.HALF_UP);
			BigDecimal updatedKucunBD = kucunBD.add(slchazhiBD).setScale(0, RoundingMode.HALF_UP);

			jxcKcmxService.setRuku(
					zcOnly,
					updatedRukuBD.toPlainString(),
					updatedKucunBD.toPlainString(),
					jxcYfk.getCompanyId()
			);
//			jxcKcmxService.setRuku(zcOnly, String.valueOf(Double.parseDouble(jxcKcmx.getZcRuku())+Double.parseDouble(slchazhi)),String.valueOf(Double.parseDouble(jxcKcmx.getZcKucun())+Double.parseDouble(slchazhi)));
		}
		//如果比原来的数量少
		if (zcShijiBD.compareTo(zcShiji1BD) < 0){
			//数量差值
			BigDecimal slchazhiBD = zcShiji1BD.subtract(zcShijiBD).setScale(0, RoundingMode.HALF_UP);
			BigDecimal currentZcShijiBD = new BigDecimal(jxcRuku.getZcShiji());
			BigDecimal updatedZcShijiBD = currentZcShijiBD.subtract(slchazhiBD).setScale(0, RoundingMode.HALF_UP);
			jxcRuku.setZcShiji(updatedZcShijiBD.toPlainString());

			jxcRuku.setZcOld(zcNew);
			jxcRukuService.updateById(jxcRuku);
			// 使用 BigDecimal 进行计算并调用 jxcKcmxService.setRuku 方法
			BigDecimal rukuBD = new BigDecimal(jxcKcmx.getZcRuku());
			BigDecimal kucunBD = new BigDecimal(jxcKcmx.getZcKucun());
			BigDecimal updatedRukuBD = rukuBD.subtract(slchazhiBD).setScale(0, RoundingMode.HALF_UP);
			BigDecimal updatedKucunBD = kucunBD.subtract(slchazhiBD).setScale(0, RoundingMode.HALF_UP);

			jxcKcmxService.setRuku(
					zcOnly,
					updatedRukuBD.toPlainString(),
					updatedKucunBD.toPlainString(),
					jxcYfk.getCompanyId()
			);
//			jxcKcmxService.setRuku(zcOnly, String.valueOf(Double.parseDouble(jxcKcmx.getZcRuku())-Double.parseDouble(slchazhi)),String.valueOf(Double.parseDouble(jxcKcmx.getZcKucun())-Double.parseDouble(slchazhi)));
		}
		//如果跟原来的数量相同
		if (Double.parseDouble(zcShiji)==Double.parseDouble(zcShiji1)){
			jxcRuku.setZcOld(zcNew);
			jxcRukuService.updateById(jxcRuku);
		}
		// 假设 zcShiji 和 zcNew 是字符串类型

		BigDecimal zcNewBD = new BigDecimal(zcNew);

		// 使用BigDecimal进行乘法运算
		BigDecimal resultBD = zcShijiBD.multiply(zcNewBD).setScale(0, RoundingMode.HALF_UP);
		String resultStr = resultBD.toPlainString();
		jxcYfk.setZcOldpay(resultStr);
		jxcYfkService.updateById(jxcYfk);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "应付款项-通过id删除")
	@ApiOperation(value="应付款项-通过id删除", notes="应付款项-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		jxcYfkService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "应付款项-批量删除")
	@ApiOperation(value="应付款项-批量删除", notes="应付款项-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.jxcYfkService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "应付款项-通过id查询")
	@ApiOperation(value="应付款项-通过id查询", notes="应付款项-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<JxcYfk> queryById(@RequestParam(name="id",required=true) String id) {
		JxcYfk jxcYfk = jxcYfkService.getById(id);
		if(jxcYfk==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(jxcYfk);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param jxcYfk
    */

    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, JxcYfk jxcYfk) {
        return super.exportXls(request, jxcYfk, JxcYfk.class, "应付款项");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */

    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, JxcYfk.class);
    }

}
